﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;


namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// PLM机台补偿配方版本更新
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PLMBCVersionUpdateController : ControllerBase
    {
        private readonly ILogger<PLMBCVersionUpdateRequestDto> _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;


        public PLMBCVersionUpdateController(ILogger<PLMBCVersionUpdateRequestDto> logger,IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration)
        {
            _logger = logger;
            _WMSTokenCheckService = wmsTokenCheckService;
             con= _configuration.GetConnectionString("DDM_DbConnection_ZH");

        }

        /// <summary>
        /// PLM机台补偿配方版本更新
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostPLMBCVersionUpdate([FromBody] PLMBCVersionUpdateRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            PLMBCVersionUpdateResponseDto re = new PLMBCVersionUpdateResponseDto();
            string ccode = strparm.ccode;

            //框架查询方法
            //var data = _WMSTokenCheckService.GetTokenCheckData(supplier_code);

            try
            {
                if (ccode.Trim() != "")
                {
                    strSql = new StringBuilder();
                    strSql.Append(@"select machine_no,work_shop,process_version,version_status,ccode,remarks,process_gradation  from plm_reparation_detail where 1=1 and ccode='" + ccode + "'");
                    DataTable dt_plm = DB.Query(strSql.ToString(), con);
                    if (dt_plm.Rows.Count> 0)
                    {
                        double version_no = 0;
                        foreach (DataRow item in dt_plm.Rows)
                        {
                            strSql = new StringBuilder();
                            strSql.Append(@"update plm_reparation_detail set version_status='失效' where  ccode<>'"+ ccode + "' and machine_no='"+ item["machine_no"].ToString() + "' and  process_gradation='"+ item["process_gradation"].ToString() + "'");
                            strSqlList.Add(strSql.ToString());
                            strSql = new StringBuilder();
                            strSql.Append(@"select max(process_version) process_version from plm_reparation_detail where  machine_no='" + item["machine_no"].ToString() + "' and ccode<>'"+ ccode + "' and   process_gradation='" + item["process_gradation"].ToString() + "'   ");
                            DataTable dt_plm_version = DB.Query(strSql.ToString(), con);
                            if (dt_plm_version.Rows.Count>0)
                            {
                                version_no = DB.ToDouble(dt_plm_version.Rows[0]["process_version"].ToString())+1;
                            }
                            else
                            {
                                version_no = 1;
                            }

                            strSql = new StringBuilder();
                            strSql.Append(@"update plm_reparation_detail set version_status='生效',process_version="+ version_no + " where  ccode='" + ccode + "' and machine_no='" + item["machine_no"].ToString() + "' and   process_gradation='" + item["process_gradation"].ToString() + "' ");
                            strSqlList.Add(strSql.ToString());
                        }

                        if (strSqlList.Count > 0)
                        {
                            bool A = DB.ExecuteNoQueryTran(strSqlList, con);
                            if (A = false)
                            {
                                strMsg = "数据库更新异常，版本更新失败";
                            }
                        }
                    }
                    else
                    {
                        strMsg = "获取不到流水号<"+ccode+">信息";
                    }

                }
            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }

         


            if (strMsg=="")
            {
                re.status = "成功";
                re.code = "0";
            }
            else
            {
                re.status = "失败";
                re.code = "500";
                re.message = strMsg;
            }


            return new JsonResult(re);
        }

      
    }
}
